﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Commons.DataFilters;
using SD.Business;
using SD.DataAccess;
using System.Globalization;

namespace SD.Web.Controls.Lookup
{
    public partial class TypeOfBusinessLookup : UserControl
    {
        //--------- Modified by [TrungL1]. Date: 14-Dec-2012. Modified comment more clearly ----------
        /// <summary>
        /// Gets or sets the text box business name id.
        /// </summary>
        /// <value>
        /// The text box business name id.
        /// </value>
        public string TextBoxBusinessNameId
        {
            get
            {
                if (ViewState["TextBoxBusinessNameId"] != null)
                {
                    return ViewState["TextBoxBusinessNameId"] as string;
                }
                return string.Empty;
            }
            set { ViewState["TextBoxBusinessNameId"] = value; }
        }

        /// <summary>
        /// Gets or sets the text box sic code id.
        /// </summary>
        /// <value>
        /// The text box sic code id.
        /// </value>
        public string TextBoxSicCodeId
        {
            get
            {
                if (ViewState["TextBoxSicCodeId"] != null)
                {
                    return ViewState["TextBoxSicCodeId"] as string;
                }
                return string.Empty;
            }
            set { ViewState["TextBoxSicCodeId"] = value; }
        }

        /// <summary>
        /// Gets or sets the hidden field business id.
        /// </summary>
        /// <value>
        /// The hidden field business id.
        /// </value>
        public string HiddenFieldBusinessId
        {
            get
            {
                if (ViewState["HiddenFieldBusinessId"] != null)
                {
                    return ViewState["HiddenFieldBusinessId"] as string;
                }
                return string.Empty;
            }
            set { ViewState["HiddenFieldBusinessId"] = value; }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var filter = new BusinessFilters();
                ViewState["filter"] = filter;
            }
        }
        /// <summary>
        /// Popups this instance.
        /// </summary>
        public void Popup()
        {
            PanelContainer.CssClass = "popup_container";
            PanelContainer.Visible = true;
        }

        /// <summary>
        /// Buttons the search click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as BusinessFilters;
            if (filter != null)
            {
                filter.BusinessName = TextBoxBusinessName.Text;
                filter.SicCode = TextBoxSISCode.Text;
            }
            GridViewSearchBusiness.DataBind();
        }

        /// <summary>
        /// Buttons the clear click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonClearClick(object sender, EventArgs e)
        {
            TextBoxBusinessName.Text = string.Empty;
            TextBoxSISCode.Text = string.Empty;
        }
        /// <summary>
        /// Buttons the none click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonNoneClick(object sender, EventArgs e)
        {
            //Author:[TrungL1]. Modified handle event when click button None. Date: 14-Dec-2012
            var businessIdControl = Parent.FindControl(HiddenFieldBusinessId) as HiddenField;
            if (businessIdControl != null)
            {
                businessIdControl.Value = string.Empty;
            }

            var businessNameControl = Parent.FindControl(TextBoxBusinessNameId) as TextBox;
            if (businessNameControl != null)
            {
                businessNameControl.Text = string.Empty;
            }

            var sicCodeControl = Parent.FindControl(TextBoxSicCodeId) as TextBox;
            if (sicCodeControl != null)
            {
                sicCodeControl.Text = string.Empty;
            }

            PanelContainer.Visible = false;

        }

        /// <summary>
        /// Buttons the select click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSelectClick(object sender, EventArgs e)
        {
            int businessId = 0;
            if (!int.TryParse(Request.Form["RadioButton"], out businessId))
            {
                return;
            }

            BusinessType objBusiness = LookupController.GetBusinessTypeById(businessId);

            var businessIdControl = Parent.FindControl(HiddenFieldBusinessId) as HiddenField;
            if (businessIdControl != null)
            {
                businessIdControl.Value = objBusiness.BusinessId.ToString(CultureInfo.CurrentCulture);
            }

            var businessNameControl = Parent.FindControl(TextBoxBusinessNameId) as TextBox;
            if (businessNameControl != null)
            {
                businessNameControl.Text = objBusiness.BusinessName;
            }

            var sicCodeControl = Parent.FindControl(TextBoxSicCodeId) as TextBox;
            if (sicCodeControl != null)
            {
                sicCodeControl.Text = objBusiness.SICCode;
            }

            PanelContainer.Visible = false;
        }

        /// <summary>
        /// Buttons the close click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonCloseClick(object sender, EventArgs e)
        {
            PanelContainer.Visible = false;
        }

        /// <summary>
        /// Handles the Selecting event of the BusinessTypeObjectDataSource control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void BusinessTypeObjectDataSource_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as BusinessFilters;
            e.InputParameters["filter"] = filter;
        }


    }
}